package com.gable.service.impl;

import com.gable.dao.UserDao;
import com.gable.init.InitDataUitl;
import com.gable.model.Screen;
import com.gable.model.User;
import com.gable.service.UserService;
import com.gable.util.MessageUtil;
import com.gable.util.ReturnResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

/**
 * Created by sjx on 2018/5/7.
 */
@Service("userService")
public class UserServiceImpl implements UserService{

    @Autowired
    UserDao userDao;

    @Value("${serviceProviderId}")
    private Long serviceProviderId;

    @Override
    public ReturnResultUtil getUsers() {
        List<User> users = userDao.getUsers();
        return new ReturnResultUtil(200, MessageUtil.QUERYSUCCESS,users);
    }

    @Override
    public ReturnResultUtil login(String username, String password) {
        ResourceBundle resource = ResourceBundle.getBundle("application");
        Long serviceProviderId = Long.valueOf(resource.getString("serviceProviderId"));
        Map<String,Object> parameterMap = new HashMap<String, Object>();	// 传参Map
        parameterMap.put("username", username);		// 账户名

        if(InitDataUitl.userNameMap.get(username) == null){
            InitDataUitl.userNameMap.put(username,1);
        }else{
            int data = InitDataUitl.userNameMap.get(username)+1;
            InitDataUitl.userNameMap.put(username,data);
        }

		/*if(InitDataUitl.userNameMap.get(username)>6){
			iUserDao.userAccountFreezing(username);
			InitDataUitl.userNameMap.put(username,0);
		}*/

        User user = userDao.queryUsernamePassword(parameterMap);
        if(user == null){
            return new ReturnResultUtil(500,"当前用户不存在");
        }else{
            if(user.getServiceProviderId()==null || user.getServiceProviderId().longValue()!=serviceProviderId.longValue()){
                return  new ReturnResultUtil(500,"该账号无权限登陆");
            }
            if(user.getStatus() == 2){
                return  new ReturnResultUtil(500,"当前帐号已被冻结，请联系管理员。");
            }
            if(!user.getPassword().equals(password)){
                return  new ReturnResultUtil(500,"密码错误");
            }
            if(user.getStatus() != null && user.getStatus() == 0){
                return  new ReturnResultUtil(500,"账户被禁用，请联系管理员。");
            }
            if(user.getInside() != 2 && user.getServiceProviderId() == serviceProviderId){
                return  new ReturnResultUtil(500,MessageUtil.LOGINERROR);
            }
            //List<Url> auth =  iRoleDao.roleAuth(user.getRoleId()).get(0).getUrlList();
            InitDataUitl.userNameMap.put(username,0);
        }
        return new ReturnResultUtil(200,"登录成功",user);
    }

    @Override
    public ReturnResultUtil queryList(Map map) {
        Screen screen = userDao.queryList(map);
        List<Screen> screens = userDao.queryCount(map);
        if(screens!=null){
            for(Screen s:screens){
                User user = userDao.queryByHospital(s.getHospitalId());
                if(user!=null){
                    s.setUserName(user.getUserName());
                    s.setPassword(user.getPassword());
                }
            }
        }
        screen.setScreens(screens);
        return new ReturnResultUtil(200, MessageUtil.QUERYSUCCESS,screen);
    }

    @Override
    public ReturnResultUtil queryRepairList(Map map) {
        List<Map> list = userDao.queryRepairList(map);
        return new ReturnResultUtil(200, MessageUtil.QUERYSUCCESS,list);
    }

    @Override
    public ReturnResultUtil queryUpkeepList(Map map) {
        List<Map> list = userDao.queryUpkeepList(map);
        return new ReturnResultUtil(200, MessageUtil.QUERYSUCCESS,list);
    }

    @Override
    public ReturnResultUtil queryInspectionList(Map map) {
        List<Map> list = userDao.queryInspectionList(map);
        return new ReturnResultUtil(200, MessageUtil.QUERYSUCCESS,list);
    }
}
